import {
  Component,
  OnInit,
  ChangeDetectionStrategy,
  NgZone,
  ChangeDetectorRef,
  AfterViewInit,
} from "@angular/core";
import { NetworkGraphService } from "./network-graph.service";
@Component({
  selector: "app-dashboard-main",
  templateUrl: "./dashboard-main.component.html",
  styleUrls: ["./dashboard-main.component.less"],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DashboardMainComponent implements OnInit, AfterViewInit {
  miserables: any;
  histroy = [];
  histroyNode: any;
  constructor(
    private cdr: ChangeDetectorRef,
    private networkGraphService: NetworkGraphService
  ) {}
  ngOnInit(): void {}
  ngAfterViewInit(): void {
    setTimeout(() => {
      this.networkGraphService.getData().then((data) => {
        // tslint:disable-next-line: variable-name
        const _miserables = {
          nodes: data[0],
          edges: data[1],
        };
        // Counts the number each nodes have with each other
        _miserables.nodes.forEach((d) => {
          d.weight = _miserables.edges.reduce((total, link) => {
            if (d.id === link.source || d.id === link.target) {
              total += 1;
            }
            return total;
          }, 0);
        });
        this.miserables = _miserables;
        this.cdr.markForCheck();
      });
    }, 0);
  }
  graphCustomEvent($event) {
    console.log("graphCustomEvent");
    console.log($event);
    if ($event.type === "nodeClick") {
      this.histroy.push($event);
      this.cdr.detectChanges();
    }
  }
  onHistroyBtnClick(index, data) {
    this.histroy = this.histroy.slice(0, index + 1);
    this.histroyNode = { ...data };
    this.cdr.detectChanges();
  }
}
